package com.xyoto.community.service.impl;

import com.xyoto.community.constant.DatabaseOperationResult;
import com.xyoto.community.constant.OssBucketModuleName;
import com.xyoto.community.dto.AddSquareModuleDTO;
import com.xyoto.community.dto.UpdateSquareModuleDTO;
import com.xyoto.community.entity.main.SquareModuleConfig;
import com.xyoto.community.enums.CommonEnum;
import com.xyoto.community.enums.FileEnum;
import com.xyoto.community.enums.SquareModuleConfigEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.SquareModuleMapper;
import com.xyoto.community.service.SquareModuleConfigService;
import com.xyoto.community.utils.Result;
import com.xyoto.community.utils.XYOtoOss;
import com.xyoto.community.vo.SquareModuleConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * @author coderFu
 * @create 2021/11/15
 */
@Service
@Slf4j
public class SquareModuleConfigServiceImpl implements SquareModuleConfigService {


    @Autowired
    private SquareModuleMapper squareModuleMapper;

    @Resource
    private XYOtoOss xyOtoOss;


    @Override
    public Result addSquareModule(AddSquareModuleDTO addSquareModuleDTO) {

        if (addSquareModuleDTO == null) {
            ExceptionUtils.error(CommonEnum.REQUEST_PARAM_ERROR);
        }

        SquareModuleConfig squareModuleConfig = new SquareModuleConfig();
        //是否存在图片
        MultipartFile file = addSquareModuleDTO.getMultipartFile();
        if (file != null) {
            //todolist 校验图片的格式
            //上传图片到OSS
            try {
                String path = xyOtoOss.uploadFileAvatar(file, OssBucketModuleName.SQUARE_MODULE_CONFIG_PICTURE);
                squareModuleConfig.setModuleImg(path);
            } catch (IOException e) {
                e.printStackTrace();
                ExceptionUtils.error(FileEnum.FILE_UPLOAD_FAIL);
            }
        }
        //存储其他信息
        squareModuleConfig.setLink(addSquareModuleDTO.getLink());
        squareModuleConfig.setMessage(addSquareModuleDTO.getMessage());
        squareModuleConfig.setModuleName(addSquareModuleDTO.getModuleName());
        squareModuleConfig.setStatus(addSquareModuleDTO.getStatus());
        //顺序确定
        //查询最大的模块id

        Integer maxModuleId = squareModuleMapper.getMaxModuleId();
        if (maxModuleId == null) {
            //当前一定是第一个
            squareModuleConfig.setOrders(1);
        } else {
            //判断当前的顺序是否大于最大值
            Integer orders = addSquareModuleDTO.getOrders();
            if (orders == null) {
                squareModuleConfig.setOrders(maxModuleId + 1);
            } else if (orders >= maxModuleId) {
                squareModuleConfig.setOrders(maxModuleId + 1);
            } else {
                //属于要交换的情况
                //查询要交换的另一半是否存在
                SquareModuleConfig squareModuleConfig1 = squareModuleMapper.getModuleConfigByOrders(orders);
                if (squareModuleConfig1 == null) {
                    log.info("奇怪你怎么被删除了");
                } else {
                    squareModuleConfig1.setOrders(maxModuleId + 1);
                    Integer integer = squareModuleMapper.updateSquareModuleOrders(squareModuleConfig1);
                    if (integer < DatabaseOperationResult.SUCCESS) {
                        log.info("另一模块更新失败");
                        ExceptionUtils.error(SquareModuleConfigEnum.SQUARE_MODULE_CONFIG_UPDATE_FAIL);
                    }
                }
                squareModuleConfig.setOrders(orders);
            }
        }

        Integer integer = squareModuleMapper.addSquareModule(squareModuleConfig);
        if (integer < DatabaseOperationResult.SUCCESS) {
            ExceptionUtils.error(SquareModuleConfigEnum.SQUARE_MODULE_CONFIG_ADD_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result updateSquareModule(UpdateSquareModuleDTO updateSquareModuleDTO) {
        if (updateSquareModuleDTO == null || updateSquareModuleDTO.getId() == null || updateSquareModuleDTO.getId() <=0){
            return Result.error(CommonEnum.REQUEST_PARAM_ERROR);
        }
        //查询源数据信息
        SquareModuleConfig squareModuleConfigSource = squareModuleMapper.getSquareModuleConfig(updateSquareModuleDTO.getId());
        //暂存图片状态，一旦有新文件，后面就可以删除之前的文件
        String moduleImg = squareModuleConfigSource.getModuleImg();
        //判断是否有新图片的修改
        MultipartFile file = updateSquareModuleDTO.getMultipartFile();
        if (file != null) {
            //有新文件上传
            try {
                String path = xyOtoOss.uploadFileAvatar(file,OssBucketModuleName.SQUARE_MODULE_CONFIG_PICTURE);
                squareModuleConfigSource.setModuleImg(path);
            } catch (IOException e) {
                e.printStackTrace();
                ExceptionUtils.error(FileEnum.FILE_UPLOAD_FAIL);
            }
        }
        Integer maxModuleId = squareModuleMapper.getMaxModuleId();
        //判断顺序是否有修改
        Integer orders = updateSquareModuleDTO.getOrders();
        if (orders != null){
            if (orders > maxModuleId) {
                ExceptionUtils.error(SquareModuleConfigEnum.SQUARE_MODULE_CONFIG_ORDERS_OUT_OF_BOUND);
            }
            if (!squareModuleConfigSource.getOrders().equals(orders)){
                //在内部互换
                //查询最大id模块中是否有人选
                SquareModuleConfig moduleConfigByOrders = squareModuleMapper.getModuleConfigByOrders(maxModuleId);
                if (moduleConfigByOrders == null){
                    log.info("最后一个没有数据了啊");
                }else {
                    moduleConfigByOrders.setOrders(squareModuleConfigSource.getOrders());
                    //更新另一位的数据
                    Integer integer = squareModuleMapper.updateSquareModuleOrders(moduleConfigByOrders);
                    if (integer< DatabaseOperationResult.SUCCESS){
                        ExceptionUtils.error(SquareModuleConfigEnum.SQUARE_MODULE_CONFIG_UPDATE_FAIL);
                    }
                }
                squareModuleConfigSource.setOrders(orders);
            }
        }
        squareModuleConfigSource.setStatus(updateSquareModuleDTO.getStatus());
        squareModuleConfigSource.setModuleName(updateSquareModuleDTO.getModuleName());
        squareModuleConfigSource.setLink(updateSquareModuleDTO.getLink());
        squareModuleConfigSource.setMessage(updateSquareModuleDTO.getMessage());
        Integer integer = squareModuleMapper.updateSquareModule(squareModuleConfigSource);
        if (integer <DatabaseOperationResult.SUCCESS){
            ExceptionUtils.error(SquareModuleConfigEnum.SQUARE_MODULE_CONFIG_UPDATE_FAIL);
        }
        //删除之前OSS中存储到图片
        if (file != null){
            //删除之前的OSS图片
            xyOtoOss.deleteFileAvatar(moduleImg);
        }
        return Result.success();
    }

    @Override
    public Result getSquareModuleList() {
        List<SquareModuleConfigVO> squareModuleConfigVoS = squareModuleMapper.getSquareModuleList();
        return Result.success(squareModuleConfigVoS);
    }

    @Override
    public Result getSquareModuleById(Integer id) {
        if (id == null || id <= 0){
            ExceptionUtils.error(CommonEnum.REQUEST_PARAM_ERROR);
        }
        SquareModuleConfig squareModuleConfig = squareModuleMapper.getSquareModuleConfig(id);
        return Result.success(squareModuleConfig);
    }
}
